package br.progweb.webapp.controller;

import br.progweb.webapp.model.Contato;
import br.progweb.webapp.service.ContatoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: luiza
 * Date: 05/09/12
 * Time: 00:44
 * To change this template use File | Settings | File Templates.
 */

@Controller
public class ContatoController {
// ------------------------------ FIELDS ------------------------------

    @Autowired
    ContatoManager contatoManager;

// -------------------------- OTHER METHODS --------------------------

    @RequestMapping("/contatos*")
    public String execute(ModelMap model) {
        model.addAttribute("contatoList", contatoManager.getContatos());
        return "contatoList";
    }

    @RequestMapping("/contatosFilter*")
    //@RequestMapping(method = RequestMethod.POST)
    public String onSubmit(Contato contato, BindingResult result, HttpServletRequest request, ModelMap model)
            throws Exception {
        if (request.getParameter("filterOption").equals("Todos"))
            model.addAttribute("contatoList", contatoManager.getContatos());

        if (!request.getParameter("filterOption").equals("Todos") && !request.getParameter("filterOption").equals(""))
            model.addAttribute("contatoList",
                    contatoManager.getContatos(request.getParameter("filterOption").toString(),
                    request.getParameter("filterValue").toString()));
        return "contatoList";
    }
}
